package top.continew.admin.controller.distribute;

import cn.hutool.core.bean.BeanUtil;
import cn.shengchao.examstar.rpc.base.PageResult;
import cn.shengchao.examstar.rpc.base.TransResult;
import cn.shengchao.examstar.rpc.distribute.bi.DistributeContentBI;
import cn.shengchao.examstar.rpc.distribute.dto.CourseNodesDistributeTree;
import cn.shengchao.examstar.rpc.distribute.dto.DistributeContentBatchDto;
import cn.shengchao.examstar.rpc.distribute.dto.DistributeContentDto;
import cn.shengchao.examstar.rpc.distribute.req.CourseDistributeQueryPage;
import cn.shengchao.examstar.rpc.distribute.req.CourseDistributeUserQueryPage;
import cn.shengchao.examstar.rpc.distribute.resp.CourseDistributeForUserRespEntity;
import cn.shengchao.examstar.rpc.distribute.resp.CourseDistributeRespEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.continew.admin.req.DistributeContentProductBatchClearRto;
import top.continew.admin.req.DistributeContentProductBatchRegisterRto;
import top.continew.admin.system.util.ValidateGroup;
import top.continew.starter.web.model.R;

import java.util.List;

/**
 * @program: examstar-manger-api
 * @description: 内容分销管理的Rest api
 * @author: Xiong weiqiang
 * @create: 2025-03-26 17:04
 */
@Tag(name = "分销-内容分销登记")
@RestController
@RequestMapping("/distribute/contentProduct")
public class DistributeContentProductController {
    @DubboReference
    DistributeContentBI distributeContentBI;

    /**
     * 查分销内容指定课程的章节树
     *
     * @param courseId
     * @param userId
     * @return
     */
    @Operation(summary = "查分销课程指定课程的章节树(带比例）", description = "查分销课程指定课程的章节树(带比例）")
    @GetMapping("/getCourseNodesTree")
    R<List<CourseNodesDistributeTree>> getCourseNodesTree(Long courseId, Long userId) {
        return R.ok(distributeContentBI.getCourseNodesTree(courseId,userId));
    }

    /**
     * 查分销内容指定课程的章节树
     *
     * @param courseId
     * @return
     */
    @Operation(summary = "查课程指定课程的章节树(带作者分销）", description = "查课程指定课程的章节树(带作者分销）")
    @GetMapping("/getCourseNodesTreeVo")
    R<List<CourseNodesDistributeTree>> getCourseNodesTreeVo(Long courseId) {
        return R.ok(distributeContentBI.getCourseNodesTreeVo(courseId));
    }
    /**
     * 分页查询分销市场经理列表
     *
     * @param pageQuery
     * @return
     */
    @Operation(summary = "分页查询内容分销商的分销课程列表", description = "分页查询内容分销商的分销课程列表")
    @GetMapping("/pageQueryForUser")
    PageResult<CourseDistributeForUserRespEntity> pageQueryByUserId(CourseDistributeUserQueryPage pageQuery) {
        return distributeContentBI.getCourseDistributeListForUser(pageQuery);
    }

    /**
     * 分页查询分销课程列表(没有关联)
     *
     * @param pageQuery
     * @return
     */
    @Operation(summary = "分页查询分销课程列表", description = "分页查询分销课程列表")
    @GetMapping("/pageQuery")
    PageResult<CourseDistributeRespEntity> pageQuery(CourseDistributeQueryPage pageQuery) {
        return distributeContentBI.getCourseDistributeList(pageQuery);
    }

    /**
     * 登记保存内容的比例
     * ({"user})
     *
     * @param productBatchRegisterRto
     * @return
     */
    @Operation(summary = "登记保存内容产品的分销比例", description = "登记保存内容产品的分销比例")
    @PostMapping("/save")
    R<String> save(@Validated(ValidateGroup.ReqGroup.SAVE.class) @RequestBody DistributeContentProductBatchRegisterRto productBatchRegisterRto) {
        DistributeContentBatchDto distributeContentBatchDto = new DistributeContentBatchDto();
        List<DistributeContentDto> distributeContentDtoList = BeanUtil.copyToList(productBatchRegisterRto.getRegisterProductList(), DistributeContentDto.class);
        distributeContentBatchDto.setAuthorUserId(productBatchRegisterRto.getUserId());
        distributeContentBatchDto.setContentDtoList(distributeContentDtoList);
        TransResult<String> transResult = distributeContentBI.distributeContentRegister(distributeContentBatchDto);
        if (!transResult.getSuccess()) {
            return R.fail(R.fail().getCode(), transResult.getMessage());
        }
        return R.ok();
    }
    /**
     * 清除已经登记的内容
     * ({"user})
     *
     * @param productBatchClearRto
     * @return
     */
    @Operation(summary = "清除内容用户已经登记的内容", description = "清除内容用户已经登记的内容")
    @PostMapping("/clearContentReg")
    R clearContentReg(@Validated @RequestBody DistributeContentProductBatchClearRto productBatchClearRto) {
        DistributeContentBatchDto distributeContentBatchDto = new DistributeContentBatchDto();
        List<DistributeContentDto> distributeContentDtoList = productBatchClearRto.getProductIdList().stream().map(productId -> {
            DistributeContentDto distributeContentDto = new DistributeContentDto();
            distributeContentDto.setProductId(productId);
            return distributeContentDto;
        }).toList();
        distributeContentBatchDto.setAuthorUserId(productBatchClearRto.getUserId());
        distributeContentBatchDto.setContentDtoList(distributeContentDtoList);
        distributeContentBI.distributeContentClear(distributeContentBatchDto);
        return R.ok();
    }
}